<?php 

class ingredient{
	public $db;
	
	public $id;
	public $name;
	public $price;
	public $usageMin;
	public $usageNormal;
	public $usageMax;
	public $stock;
	public $image;
	function __construct() 
	{
		$this->db = new database();
	}
	
	function toDB($id = false)
	{
		if(!$id) {
			$insert_id = $this->db->insert("INSERT INTO `ingredient` VALUES(NULL,'".$this->name."', '".$this->price."', '".$this->usageMin."', '".$this->usageNormal."', '".$this->usageMax."', '".$this->stock."', '".$this->image."')");
				if(!insert_id) {
					
					return false;
				} else {
					
					return $insert_id;
				}
		} else {
			if($this->db->update("UPDATE `ingredient` SET `name`='".$this->name."', `price`='".$this->price."', `stock`='".$this->stock."', `usageMin`='".$this->usageMin."', `usageNormal`='".$this->usageNormal."', `usageMax`='".$this->usageMax."', `image`='".$this->image."' WHERE `id`='".$this->id."'")) {
				
				return true;
			} else {
				
				return false;
			}
		}
	}
	function load($id)
	{
		$result = $this->db->select("SELECT * FROM `ingredient` WHERE `ID`='".$id."'");
		
		if(mysql_num_rows($result) != 1) {
			
			return false;
		} else {
			$row = mysql_fetch_array($result);
			$this->id = $id;
			$this->name = $row['name'];
			$this->price = $row['price'];
			$this->usageMin = $row['usageMin'];
			$this->usageNormal = $row['usageNormal'];
			$this->usageMax = $row['usageMax'];
			$this->stock = $row['stock'];
			$this->image = $row['image'];
		}
	}
	function fromArray($row)
	{
		$this->id = $row['ID'];
		$this->name = $row['name'];
		$this->price = $row['price'];
		$this->usageMin = $row['usageMin'];
		$this->usageNormal = $row['usageNormal'];
		$this->usageMax = $row['usageMax'];
		$this->stock = $row['stock'];
		$this->image = $row['image'];
	}
	
	function validateField($field, $value)
	{
		switch ($field) {
			case "name":
				if($value == "") {
					return false;
				} else {
					return true;
				}
			break;
			case "price":
				if($value == "" || !is_numeric($value)) {
					return false;
				} else {
					return true;
				}
			break;
			case "usage":
				if($value == "" || !is_numeric($value)) {
					
					return false;
				} else {
					return true;
				}
			break;
			case "stock":
				if($value == "" || !is_numeric($value)) {
					return false;
				} else {
					return true;
				}
			break;
		}
	}
}
?>